import {
  genderOptions,
  yesNoTypeOptions,
  measureTypeOptions,
  houseSourceOptions,
  fileTypeOptions,
  houseTypeOptions,
  currencyOptions,
  ownerSource,
  ownerSourceT,
  houseStatusOptions,
  publishStatusOptions,
  staListNew,
  staListNewSF,
  staListNewBG,
  staListNewWX,
  staListNewJF,
  staListNewWX1
} from '@/utils/consts'
import moment from 'moment'
import { CodeToText } from 'element-china-area-data'

// 获取文件后缀名
export function getFileSuffix(file) {
  let fileType = ''
  const typeArr = file.name.split('.')
  const arrLen = typeArr.length
  fileType = typeArr[arrLen - 1]
  return fileType
}
// 获取文件后缀名
export function getFileSuffixMp4(file) {
  let fileType = ''
  const typeArr = file.split('/')
  const arrLen = typeArr.length
  fileType = typeArr[arrLen - 1]
  return fileType
}

// 获取文件后缀名
export function getFileSuffixSimple(file) {
  let fileType = ''
  const typeArr = file.split('.')
  const arrLen = typeArr.length
  fileType = typeArr[arrLen - 1]
  return fileType
}

// 下载
export function downImg(path, name) {
  var x = new XMLHttpRequest()
  var resourceUrl = path
  x.open('GET', resourceUrl, true)
  x.responseType = 'blob'
  x.onload = function(e) {
    var url = window.URL.createObjectURL(x.response)
    var a = document.createElement('a')
    a.href = url
    a.download = name
    a.click()
  }
  x.send()
}
/**
 * 检查参数，查询列表的时候空字符串("")统一替换成null
 * @returns {*}
 */
export function replaceParamEmptyString(param, filterKeys) {
  for (let i = 0; i < filterKeys.length; i++) {
    const fk = filterKeys[i]
    if (param[fk] !== undefined && param[fk] === '') {
      param[fk] = null
    }
  }
  return param
}

export function listenWindow() {
  const height = 250 // height主要用来计算页面中除了表格外其他部分的固定的高度
  const tableHeight = document.getElementById('app').offsetHeight - height
  // 如果当前需求为变化的表格高度最小为350px，可以按照如下设置
  // listTableHeight即页面变化后表格应该有的最大高度
  const listTableHeight = tableHeight <= 350 ? 350 : tableHeight
  return listTableHeight
}

function dataFormat(val, options) {
  let format = '未知'
  if (val == null || val === '') return format

  for (let i = 0; i < options.length; i++) {
    //  console.log('val:', val)
    // console.log('options[i].value:', options[i].value)
    if (val === options[i].value) {
      format = options[i].label
      break
    }
  }
  return format
}
export function dateFormatRow(row) {
  if (row.createdTime === undefined) {
    return ''
  }
  return moment(row.createdTime).format('YYYY-MM-DD')
}
export function dateFormatmonth(row) {
  if (row.createdTime === undefined) {
    return ''
  }
  return moment(row.createdTime).format('YYYY-MM')
}
export function billDateFormat(billDate) {
  if (billDate === undefined) {
    return ''
  }
  return moment(billDate).format('MM/DD')
}
export function timeFormat(billDate) {
  if (billDate === undefined) {
    return ''
  }
  return moment(billDate).format('MM-DD HH:mm')
}
export function myTime(row) {
  if (row.createdTime === undefined) {
    return ''
  }
  return moment(row.createdTime).format('YYYY/MM/DD')
}

export function myTimestartDate(row) {
  if (row.startDate === undefined) {
    return ''
  }
  return moment(row.startDate).format('YYYY/MM/DD')
}
export function myTimeendDate(row) {
  if (row.endDate === undefined) {
    return ''
  }
  return moment(row.endDate).format('YYYY/MM/DD')
}

export function myTimeDian(row) {
  if (!row.createdTime) {
    return ''
  }
  return moment(row.createdTime).format('YYYY.MM.DD')
}

export function myTimeDianpayDate(row) {
  if (!row.payDate) {
    return ''
  }
  return moment(row.payDate).format('YYYY.MM.DD')
}

export function myTimeDianissueDate(row) {
  if (!row.issueDate) {
    return ''
  }
  return moment(row.issueDate).format('YYYY.MM.DD')
}

export function myTimeDiancheckDate(row) {
  if (!row.checkDate) {
    return ''
  }
  return moment(row.checkDate).format('YYYY.MM.DD')
}

export function myTimeDiancompleteTime(row) {
  if (!row.completeTime) {
    return ''
  }
  return moment(row.completeTime).format('YYYY.MM.DD')
}

export function myTimeDiandeliveryDate(row) {
  if (!row.deliveryDate) {
    return ''
  }
  return moment(row.deliveryDate).format('YYYY.MM.DD')
}

export function myTimeDianbeginDate(row) {
  if (!row.beginDate) {
    return ''
  }
  return moment(row.beginDate).format('YYYY.MM.DD')
}
export function myTimeDianendDate(row) {
  if (!row.endDate) {
    return ''
  }
  return moment(row.endDate).format('YYYY.MM.DD')
}
export function formatDot(data) {
  if (!data) {
    return ''
  }
  if (data % 1 === 0) {
    return moment(data).format('YYYY.MM.DD')
  } else {
    return data
  }
}
export function formatDotBill(data) {
  if (!data) {
    return ''
  }
  return moment(data).format('YYYY.MM.DD')
}
export function formatHours(data) {
  if (!data) {
    return ''
  }
  return moment(data).format('YYYY/MM/DD HH:mm')
}
export function formatAll(data) {
  if (!data) {
    return ''
  }
  return moment(data).format('YYYY-MM-DD HH:mm:ss')
}
export function dateFormat(val) {
  if (!val) {
    return ''
  }
  return moment(val).format('YYYY-MM-DD')
}
export function dateFormat2(val) {
  if (!val) {
    return ''
  }
  return moment(val).format('YYYY/MM/DD')
}
export function dateFormat3(val) {
  if (!val) {
    return ''
  }
  return moment(val).format('YYYY-MM')
}

export function formatMonth(data) {
  if (!data) {
    return ''
  }
  return moment(data).format('YYYY-MM')
}

export function diffDates(start, end) {
  if (start === undefined || end === undefined) {
    return '0'
  }
  const startDate = moment(start)
  const endDate = moment(end)

  return endDate.diff(startDate, 'days')
}

export function genderFormat(row) {
  return dataFormat(row.gender, genderOptions)
}
export function genderFormat2(gender) {
  return dataFormat(gender, genderOptions)
}
export function publishStatusFormat(row) {
  return dataFormat(row.status, publishStatusOptions)
}
export function measureTypeFormat(row) {
  return dataFormat(row.measureType, measureTypeOptions)
}
export function measureTypeFormat2(measureType) {
  return dataFormat(measureType, measureTypeOptions)
}
export function houseSourceFormat(houseSource) {
  console.log('---objHtml.houseSource:', houseSource)
  return dataFormat(houseSource, houseSourceOptions)
}
export function fileTypeFormat(row) {
  return dataFormat(row.fileType, fileTypeOptions)
}
export function houseTypeFormat(row) {
  return dataFormat(row.type, houseTypeOptions)
}
export function houseTypeFormat2(value) {
  return dataFormat(value, houseTypeOptions)
}

export function currencyFormat(row) {
  return dataFormat(row.currency, currencyOptions)
}

export function houseStatusFormat(row) {
  return dataFormat(row.status, houseStatusOptions)
}
export function houseStatusFormat2(value) {
  return dataFormat(value, houseStatusOptions)
}

export function yesNoTypeFormat(row) {
  return dataFormat(row.status, yesNoTypeOptions)
}

export function ownerSourceFormat(row) {
  return dataFormat(row.source, ownerSource)
}
export function ownerSourceFormatT(row) {
  return dataFormat(row.source, ownerSourceT)
}
export function houseTagFormat(row) {
  console.log('row.tag:', row.tag)
  return row.tag === null ? '' : row.tag.replace(new RegExp(',', 'gm'), '/')
}

export function getCodeToText(codeStr, codeArray) {
  // if (codeStr === null && codeArray === null) {
  //   return null
  // } else if (codeArray === null) {
  //   codeArray = codeStr.split(',')
  // }
  console.log(codeArray, 66)
  let area = ''
  switch (codeArray.length) {
    case 1:
      area += CodeToText[codeArray[0]]
      break
    case 2:
      area += CodeToText[codeArray[0]] + '/' + CodeToText[codeArray[1]]
      break
    case 3:
      area +=
        CodeToText[codeArray[0]] +
        '/' +
        CodeToText[codeArray[1]] +
        '/' +
        CodeToText[codeArray[2]]
      break
    default:
      break
  }
  return area
}
export var getFileList = files => {
  if (!files) {
    return []
  }
  var fileArr = files.split(',')
  var res = fileArr.map(item => {
    var index = item.lastIndexOf('/')
    var text = item.substring(index + 1, item.length)
    return text
  })
  return res
}

export function staFormat(row) {
  return dataFormat(row.status, staListNew)
}
export function staFormatSF(row) {
  return dataFormat(row.status, staListNewSF)
}
export function staFormatBG(row) {
  return dataFormat(row.type, staListNewBG)
}
export function staFormatWX(row) {
  return dataFormat(row.status, staListNewWX)
}
export function staFormatJF(row) {
  return dataFormat(row.type, staListNewJF)
}

export function staFormatJF1(row) {
  return dataFormat(row.status, staListNewWX1)
}

export function getYearList() {
  const thisYear = moment().year()
  const yearList = []
  for (let i = thisYear - 5; i <= thisYear + 10; i++) {
    yearList.push(`${i}-${i + 1}`)
  }
  return yearList
}

export function getYear(val) {
  if (val === undefined) {
    return ''
  }
  const year = moment(val).get('year')
  return `${year}-${year + 1}`
}

export function yearToDate(val) {
  if (val === undefined) {
    return ''
  }
  moment().toDate()
  return new Date(moment(`${val}-01-01`))
}

export function formatWeekday(data) {
  if (!data) {
    return ''
  }
  return moment(data)
    .locale('zh-cn')
    .format('dddd')
}
